Scroll Progress Bar

Loops are used to repeat a block of code multiple times until a certain condition is met. There are three types of loops available in C:

for Loop:

The for loop is used when know the number of iterations beforehand. It consists of three parts: initialization, condition, and update.

Syntax:

for (initialization; condition; update) {
    // Code to be repeated
}
while Loop:

The while loop is used when the number of iterations is not known beforehand, and it will continue as long as the condition is true.

Syntax:

while (condition) {
    // Code to be repeated
}
do-while Loop:

The do-while loop is similar to the while loop, but it ensures that the code inside the loop is executed at least once before checking the condition.

Syntax:

do {
    // Code to be repeated
} while (condition);
Infinite Loops:

Be careful when using loops to avoid infinite loops, where the condition is always true, and the loop never terminates. For example:


while (1) {
    // Infinite loop
}

Infinite loops should be avoided, but in some cases, they can be intentionally used, like in server programs or certain embedded systems.

Loops are essential constructs in programming, allowing to perform repetitive tasks efficiently. They are used in various scenarios, such as reading multiple inputs, processing data arrays, and executing tasks based on specific conditions.


What is a loop that repeats a block of code while a condition is true?


While

What loop is guaranteed to execute at least once?


Do-While

What loop is used for iterating over a range of values a fixed number of times?


For

What statement is used to exit a loop prematurely?


Break

What statement is used to skip the current iteration and continue to the next in a loop?


Continue